﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Portal.Interface;
using MyLib;

using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Globalization;
namespace WebPortal
{
    public partial class index : System.Web.UI.Page
    {
        IOrg _orgSrv = ServiceContainer.Instance().GetService<IOrg>();
        public string Login_UserOrPasswordInvalid = Resources.lang.Login_UserOrPasswordInvalid;
        public string Login_UserRequired = Resources.lang.Login_UserRequired;
        protected void Page_Load(object sender, EventArgs e)
        {
            userValidate();
        }
        //单点登录：portal传过来的userId通过调用BPM webservice 加密， 地址： http://10.1.29.18:90/template/ssoLogin.asmx  调用GetEnUserId方法传入userId
        //1.直接转到BPM链接：http://10.1.29.18/index.aspx?userId=1271 
        //2.portal  待办任务明细转BPM链接：http://10.0.2.15/index.aspx?userId=1271&TaskId=06091863d04b7da0fd30929c762596
        private void userValidate()
        {
            string user = Request.QueryString["userId"];

            if (user.IndexOf('Z') == 0)
            {
                HttpContext.Current.Session["UserEntity"] = null;//清除用户信息
                HttpContext.Current.Session["userAccount"] = "ULTIMUS\\" + user;
                FormsAuthentication.SetAuthCookie("ULTIMUS\\" + user, false);
                string TaskId = Request.QueryString["TaskId"];
                if (TaskId != null)
                {
                    Response.Redirect("/Modules/Workflow/OpenForm.aspx?TaskId=" + TaskId);
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }

            if (user == null)
            {
                Response.Redirect("Login.aspx");
            }
            localhost.ssoLogin sso = new localhost.ssoLogin();//调用webservice
            //user = sso.GetEnUserId(user);//用户账号对称加密
            if (ConvertUtil.IsInt(user))
            {
            }
            else
            {
                user = sso.GetDeUserId(user); //对称解密
            }

            user = "ULTIMUS\\" + user;
            if (!_orgSrv.CheckUser(user, ""))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ce", "checkError();", true);
            }

            else
            {
                HttpContext.Current.Session["UserEntity"] = null;//清除用户信息
                HttpContext.Current.Session["userAccount"] = user;
                FormsAuthentication.SetAuthCookie(user, false);
                string TaskId = Request.QueryString["TaskId"];
                if (TaskId != null)
                {
                    Response.Redirect("/Modules/Workflow/OpenForm.aspx?TaskId=" + TaskId);
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }

            }
        }


    }
}